package com.fofia.iotweb.web.controllers.admin;

import com.fofia.iotweb.Contants.SolonBootConfigDto;
import com.fofia.iotweb.cache.UserCache;
import com.fofia.iotweb.framework.core.BaseController;
import com.fofia.iotweb.framework.core.RequestHelper;
import com.fofia.iotweb.framework.dto.ZtreeDto;
import com.fofia.iotweb.web.dbdomain.mappers.admin.SysDeptMapper;
import com.fofia.iotweb.web.dbdomain.models.admin.SysDept;
import com.fofia.iotweb.web.dbdomain.services.admin.SysDeptService;
import com.fofia.iotweb.web.dto.JsonResult;
import com.fofia.iotweb.web.dto.ResultAdaptDto;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.ModelAndView;

import java.util.List;


@Mapping("admin/dept")
@Controller
public class DeptController extends BaseController {

    @Inject
    private RequestHelper requestHelper;
    @Inject
    private SysDeptService deptService;
    @Inject
    private UserCache userCache;
    @Inject
    private SolonBootConfigDto solonBootConfigDto;

    @AuthPermissions("admin.dept.view")
    @Get
    @Mapping("index")
    public ModelAndView index(){
        return view();
    }

    @AuthPermissions("admin.dept.view")
    @Get
    @Mapping("getDeptTree")
    public List<SysDept> getDeptTree(){
        String deptName=requestHelper.getString("deptName","");
        List<SysDept> list=deptService.getList(deptName);

        return list;
    }

    @AuthPermissions("admin.dept.add;admin.dept.edit")
    @Get
    @Mapping("form")
    public ModelAndView form(){
        int id=requestHelper.getInt("id",0);

        return view().put("id",id);
    }


    /**
     *
     * @author yushuo
     * @description 获取实体
     * @date 13:42 2021/2/26
     * @param [id]
     * @return com.easyboot.framework.web.ActionResult
     **/
    @AuthPermissions("admin.dept.add;admin.dept.edit")
    @Get
    @Mapping("getModel")
    public ResultAdaptDto getModel(){
        int id=requestHelper.getInt("id",0);
        ResultAdaptDto ret= JsonResult.success();
        if(id>0){
            SysDept dept=deptService.getModel(id);
            ret.put("model",dept);
        }
        List<ZtreeDto> data= deptService.getDeptZtree();

        ret.put("ztreeData",data);

        return ret;
    }

    /**
     *
     * @author yushuo
     * @description 保存
     * @date 14:15 2021/2/26
     * @param [dto]
     * @return com.easyboot.framework.web.ActionResult
     **/
    @AuthPermissions("admin.dept.add;admin.dept.edit")
    @Post
    @Mapping("save")
    public ResultAdaptDto save(SysDept dto){

        deptService.save(dto, userCache.getUserSeesion());
        ResultAdaptDto ret= JsonResult.success("部门数据已保存");
        return ret;
    }

    /**
     *
     * @author yushuo
     * @description 获取部门下来树形菜单
     * @date 14:15 2021/2/26
     * @param []
     * @return com.easyboot.framework.web.ActionResult
     **/
    @AuthPermissions("#")
    @Get
    @Mapping("getDeptZtree")
    public ResultAdaptDto getDeptZtree(){
        ResultAdaptDto ret=new ResultAdaptDto();
        List<ZtreeDto> data= deptService.getDeptZtree();

        ret.put("ztreeData",data);
        return ret;
    }

    @AuthPermissions("admin.dept.delete")
    @Get
    @Mapping("delete")
    public ResultAdaptDto delete(){
        int id=requestHelper.getInt("id",0);
        if(id==0){
            return JsonResult.failure("删除数据失败");
        }

        ResultAdaptDto ret= deptService.delete(id);
        return ret;
    }

}
